NAME 									?= LCD
TOOL 									?= arm-linux-gnueabihf-


CC 											:= $(TOOL)gcc
LD 											:= $(TOOL)ld
LD_ADDR 							 := 0x87800000
OBJCOPY 							 := $(TOOL)objcopy
OBJDUMP 							 := $(TOOL)objdump

LDFLAGS:= -lgcc -L/usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/4.9.4

INCLUDIRS							:=   imx6u \
														bsp/clk \
														bsp/delay \
														bsp/led \
														bsp/key \
														bsp/beep \
														bsp/int \
														bsp/gpio \
														bsp/EPIT \
														bsp/GPT \
														bsp/UART \
														stdio/include \
														bsp/RGB888 \
														project

SRCDIRS									:=  imx6u \
														bsp/clk \
														bsp/delay \
														bsp/led \
														bsp/key \
														bsp/beep \
														bsp/int \
														bsp/gpio \
														bsp/EPIT \
														bsp/GPT \
														bsp/UART \
														stdio/lib \
														bsp/RGB888 \
														project

INCLUDE									:= $(patsubst %, -I %,$(INCLUDIRS)) 																#将INCLUDIRS(参数3)符合参数1(%)的所有子串替换成参数2(-I %)

SFILES									 :=$(foreach dir,$(SRCDIRS),$(wildcard $(dir)/*.s))								#脚本语言的for循环，用dir遍历SRCDIRS的子串，每个子串执行参数3
CFILES									 :=$(foreach dir,$(SRCDIRS),$(wildcard $(dir)/*.c))								#wildcard $(dir)/*.c：生成指定目录下所有.c文件名(名字里包含了相对路径)

SFILENDIR							:=$(notdir $(SFILES))																								#此函数去除路径名
CFILENDIR							:=$(notdir $(CFILES))																								#此函数去除路径名

SOBJS									:=$(patsubst %, obj/%,$(SFILENDIR:.s=.o))													#$(SFILENDIR:.s=.o)将所有.s替换成.o后缀，然后匹配上参数1的子串都会变成obj/...	
COBJS									:=$(patsubst %, obj/%,$(CFILENDIR:.c=.o))													#$(SFILENDIR:.s=.o)将所有.c替换成.o后缀，然后匹配上参数1的子串都会变成obj/...
OBJS									 :=$(SOBJS)$(COBJS)

VPATH                 					:=$(SRCDIRS)																													#如果指定了VPATH，则会自动在路经里找文件作依赖


$(NAME).bin:$(OBJS)
	$(LD) -Timx6u.lds $^ -o $(NAME).elf $(LDFLAGS)
	$(OBJCOPY) -O binary -S $(NAME).elf $@
	$(OBJDUMP) -D $(NAME).elf > $(NAME).dis

obj/%.o : %.s																																								#OBJS作为目标，匹配到的字符串均为obj/%.o，%仅仅匹配当前目录下的文件
	$(CC) -Wall -nostdlib -fno-builtin -Wa,-mimplicit-it=thumb -c  -Ofast $(INCLUDE) -o $@ $<																															
obj/%.o : %.c
	$(CC) -Wall -nostdlib -fno-builtin -Wa,-mimplicit-it=thumb  -c  -Ofast $(INCLUDE) -o $@ $<

clear:
	rm $(NAME).bin $(NAME).elf obj/*.o $(NAME).dis

print:
	@echo INCLUDE=$(INCLUDE)
	@echo SFILES=$(SFILES)
	@echo CFILES=$(CFILES)
	@echo SFILENDIR=$(SFILENDIR)
	@echo CFILENDIR=$(CFILENDIR)
	@echo SOBJS=$(SOBJS)
	@echo COBJS=$(COBJS)
	@echo OBJS=$(OBJS)